package by.yakusekmaxim.bookgid.webapp.page.admin.series;

import java.util.Iterator;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import by.yakusekmaxim.bookgid.dataaccess.util.Filter;
import by.yakusekmaxim.bookgid.datamodel.Book;
import by.yakusekmaxim.bookgid.datamodel.Book_;
import by.yakusekmaxim.bookgid.datamodel.Series;
import by.yakusekmaxim.bookgid.datamodel.Series_;
import by.yakusekmaxim.bookgid.services.BookService;
import by.yakusekmaxim.bookgid.services.SeriesService;
import by.yakusekmaxim.bookgid.webapp.page.admin.book.BookPage;

public class SeriesListPanel extends Panel {

	@Inject
	private SeriesService seriesService;

	@Inject
	private BookService bookService;

	public SeriesListPanel(String id) {
		super(id);
		SeriesDataProvider authorDataProvider = new SeriesDataProvider();

		DataView<Series> dataView = new DataView<Series>("list",
				authorDataProvider, 10) {
			@Override
			protected void populateItem(Item<Series> item) {
				final Series series = item.getModelObject();
				item.add(new Label("id"));
				item.add(new Label("name"));

				Link<Object> linkAuthor = new Link<Object>("linkBook") {
					@Override
					public void onClick() {
						Filter<Book> filter = new Filter<Book>();
						filter.setEnabled(true);
						filter.setAttribute(Book_.series);
						filter.setValue(series);
						setResponsePage(new BookPage(filter, new ResourceModel(
								"p.admin.series.bookLink").getObject()
								+ series.getName()));

					}
				};

				linkAuthor.setEnabled(bookService
						.getCount(Book_.series, series) != 0);
				linkAuthor.add(new Label("bookCount", bookService.getCount(
						Book_.series, series)));
				item.add(linkAuthor);
				item.add(new Link<Object>("edit") {
					@Override
					public void onClick() {
						setResponsePage(new EditSeriesPage(series));
					}
				});
				item.add(new Link<Object>("delete") {
					@Override
					public void onClick() {
						seriesService.delete(series);
						setResponsePage(new SeriesPage());
					}
				});
			}

		};

		add(dataView);

		add(new PagingNavigator("paging", dataView));

		add(new OrderByBorder<SingularAttribute<Series, ?>>("sortByID",
				Series_.id, authorDataProvider));
		add(new OrderByBorder<SingularAttribute<Series, ?>>("sortByName",
				Series_.name, authorDataProvider));

	}

	private class SeriesDataProvider extends
			SortableDataProvider<Series, SingularAttribute<Series, ?>> {

		public SeriesDataProvider() {
			super();
			setSort(Series_.id, SortOrder.ASCENDING);

		}

		@Override
		public Iterator<? extends Series> iterator(long first, long count) {

			SingularAttribute<Series, ?> sortParam = getSort().getProperty();
			SortOrder propertySortOrder = getSortState().getPropertySortOrder(
					sortParam);
			boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);

			return seriesService.getAllSeries(sortParam, ascending,
					(int) first, (int) count).iterator();
		}

		@Override
		public long size() {

			return seriesService.getCount();
		}

		@Override
		public IModel<Series> model(Series series) {
			return new CompoundPropertyModel<Series>(series);
		}
	}
}
